using System;
using System.Data;
using System.IO;
using Agile.Common;

namespace Agile.Genie.Descriptors
{
    /// <summary>
    /// Summary description for VisualStudioProjectFile2005.
    /// </summary>
    public class VisualStudioProjectFile2005 : VisualStudioProjectFile
    {
        #region Constructors and Factories

        /// <summary>
        /// Construct with a project file.
        /// </summary>
        /// <param name="projectFile">The project file to instantiate with.</param>
        protected VisualStudioProjectFile2005(FileInfo projectFile)
            : base(projectFile)
        {
            if (VisualStudioVersion != "2005")
                throw new AgileCommonException("Project file is not a VS2005 project file.");
        }

        /// <summary>
        /// Instantiate a new VisualStudioProjectFile with the given project file.
        /// </summary>
        /// <param name="projectFile">The project file to instantiate with.</param>
        /// <returns></returns>
        public static VisualStudioProjectFile2005 Build(FileInfo projectFile)
        {
            return new VisualStudioProjectFile2005(projectFile);
        }

        /// <summary>
        /// Instantiate a new VisualStudioProjectFile with the path to a project file.
        /// </summary>
        /// <param name="projectFilePath">The path of a project file.</param>
        /// <returns></returns>
        public static VisualStudioProjectFile2005 Build(string projectFilePath)
        {
            return Build(new FileInfo(projectFilePath));
        }

        #endregion

        #region Overrides

        /// <summary>
        /// Gets the path (including the application name) of the visual studio
        /// version that needs to be used to open the project file.
        /// </summary>
        public override string VisualStudioApplicationPath
        {
            get { return @"${directory::get-parent-directory(environment::get-variable('VS80COMNTOOLS'))}\IDE\devenv.exe"; }
        }

        /// <summary>
        /// Gets the Assembly name of the project.
        /// </summary>
        public override string ProjectAssemblyName
        {
            get { return ProjectFileData.Tables["PropertyGroup"].Rows[0]["AssemblyName"].ToString(); }
        }

        /// <summary>
        /// Gets the projects GUID.
        /// </summary>
        public override Guid ProjectGuid
        {
            get
            {
                Guid projectGuid =
                    CommonFunctions.SafeGuid(ProjectFileData.Tables["PropertyGroup"].Rows[0]["ProjectGuid"]);
                return projectGuid;
            }
        }

        /// <summary>
        /// Gets the projects output type, e.g. Exe or library
        /// </summary>
        public override string OutputType
        {
            get { return ProjectFileData.Tables["PropertyGroup"].Rows[0]["OutputType"].ToString(); }
        }

        /// <summary>
        /// Gets the type of project, e.g. Web or Local
        /// </summary>
        public override string PlatformFamilyName
        {
            get
            {
                if (!ProjectFileData.Tables["PropertyGroup"].Columns.Contains("PlatformFamilyName"))
                    return string.Empty;

                return ProjectFileData.Tables["PropertyGroup"].Rows[0]["PlatformFamilyName"].ToString();
            }
        }

        /// <summary>
        /// Gets the rows from the dataset that relate specifically to references to 
        /// other projects.
        /// </summary>
        protected override DataTable ProjectReferenceRows
        {
            get
            {
                if (!ProjectFileData.Tables.Contains("ProjectReference"))
                    return new DataTable("NoProjectFilesReferenced");

                return ProjectFileData.Tables["ProjectReference"];
            }
        }

        /// <summary>
        /// Gets the Assembly name from the given Reference table row.
        /// </summary>
        /// <param name="referenceTableRow">A row from the Reference table.</param>
        /// <returns></returns>
        public override string GetReferencedAssemblyName(DataRow referenceTableRow)
        {
            // We only want the name of the file so get the index of the first comma
            string includeText = referenceTableRow["Include"].ToString();
            int endIndex = includeText.IndexOf(",");
            if (endIndex > -1)
                return includeText.Substring(0, endIndex);
            return includeText;
        }

        #endregion
    }
}